﻿using DatabaseAccess;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;

namespace Liguo.AppCode
{
    public static class JsonConvert
    {
        public static string Serialize<T>(T obj)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, obj);
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }

        public static T Deserialise<T>(string json)
        {
            T obj = Activator.CreateInstance<T>();
            using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                obj = (T)serializer.ReadObject(ms);
                return obj;
            }
        }

        public static String GetGoodsInfoJson(int id)
        {
            SqlIt sql = new SqlIt();
            String feild = "GoodsName,CartInfo.GoodsID,Price,CartInfo.Amount,CartInfo.Weight,PicturePath,SaleMethod";
            String table = "GoodsInfo,CartInfo";
            String condition = "CartInfo.UserID='" + id + "' and CartInfo.GoodsID=GoodsInfo.ID";
            DataSet ds = sql.SpecialItem(table, condition, feild);
            int count = ds.Tables[0].Rows.Count;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                sb.Append("{");
                sb.Append("\"id\":").Append(ds.Tables[0].Rows[i]["GoodsID"]);
                sb.Append(",");
                sb.Append("\"name\":\"").Append(ds.Tables[0].Rows[i]["GoodsName"]).Append("\"");
                sb.Append(",");
                sb.Append("\"img\":").Append(ds.Tables[0].Rows[i]["PicturePath"]);
                sb.Append(",");
                int saleM = Convert.ToInt32(ds.Tables[0].Rows[i]["SaleMethod"]);
                sb.Append("\"method\":").Append(saleM);
                sb.Append(",");
                //整卖整算
                if (saleM == 0)
                {
                    int amount = Convert.ToInt32(ds.Tables[0].Rows[i]["Amount"]);
                    double price = amount * Convert.ToDouble(ds.Tables[0].Rows[i]["Price"]);
                    sb.Append("\"num\":").Append(amount);
                    sb.Append(",");
                    sb.Append("\"price\":").Append(price);
                }
                //整卖散算 价格未知
                else if (saleM == 1)
                {
                    int amount = Convert.ToInt32(ds.Tables[0].Rows[i]["Amount"]);
                    sb.Append("\"num\":").Append(amount);
                    sb.Append(",");
                    sb.Append("\"price\":").Append(0);
                }
                //散卖散算
                else if (saleM == 2)
                {
                    double weight = Convert.ToDouble(ds.Tables[0].Rows[i]["Weight"]);
                    double price = weight * Convert.ToDouble(ds.Tables[0].Rows[i]["Price"]);
                    sb.Append("\"num\":").Append(weight);
                    sb.Append(",");
                    sb.Append("\"price\":").Append(price);
                }
                //散卖整算（其实没这个）
                else
                {
                    sb.Append("\"num\":").Append(0);
                    sb.Append(",");
                    sb.Append("\"price\":").Append(0);
                }
                sb.Append("}");
                if (i != count - 1)
                {
                    sb.Append(",");
                }
            }
            return sb.ToString();
        }
    }
}